class_name ObjectPool 
extends Resource

# 对象池物体可以选择实现以下方法：
# init_pool_object(args:Array)
# get_from_pool()
# back_to_pool()

@export var pool_name:String
@export var pool_max_size:int
@export var pool_scene:PackedScene
@export var pool_extend_value:float = 2
var pool_manager:ObjectPoolManager
var object_parent:Node
var pool_queue = []

func init_pool(pool_manager:ObjectPoolManager):
	self.pool_manager = pool_manager
	self.object_parent = pool_manager.object_parent
func create_scene():
	var scene = pool_scene.instantiate()
	object_parent.add_child(scene)
	scene.visible = false
	if "init_pool_object" in scene:
		scene.init_pool_object([pool_manager.pool_manager_name,pool_name])
	return scene
func fill_pool():
	for i in range(pool_max_size):
		var scene = create_scene()
		pool_queue.push_back(scene)
	pool_max_size *= pool_extend_value
func get_from_pool():
	var scene
	if pool_queue.size() <= 0:
		fill_pool()
	scene = pool_queue.pop_front()
	if "get_from_pool" in scene:
		scene.get_from_pool()
	return scene
func back_to_pool(scene):
	if pool_queue.size() == pool_max_size:
		scene.queue_free()
	else:
		if "back_to_pool" in scene:
			scene.back_to_pool()
		pool_queue.push_back(scene)
